#include "Character.h"
#include <BulletCollision/CollisionDispatch/btGhostObject.h>
#include "PhysicManager.h"
#include "Keyboard.h"
#include "Game.h"
#include "Mouse.h"
#include <Math.h>

Character::Character()
{

}

Character::Character(const Character&)
{

}

Character::~Character()
{

}

Character Character::operator=(Character const&)
{
	return Character();
}

bool Character::Initialize(Model3D *pModel)
{
	mGame = Game::Instance();
	mWalkSpeed = 0;
	mBodyOrientation = 0;
	mHeadOrientation = 0;

	btTransform startTransform;
	startTransform.setIdentity ();
	startTransform.setOrigin (btVector3(00.0, 1.0, 00.0));

	//mGhostObject = new btPairCachingGhostObject();
	//mGhostObject->setWorldTransform(startTransform);
	
	btScalar characterHeight= 1.10;
	btScalar characterWidth =0.35;

	btCollisionShape* mPhysicShape = new btCapsuleShape(characterWidth, characterHeight);

	//mGhostObject->setCollisionShape (capsule);
	//mGhostObject->setCollisionFlags (btCollisionObject::CF_CHARACTER_OBJECT);
	//mGhostObject->setUserPointer(this);

	//btScalar stepHeight = btScalar(0.35);
	//btKinematicCharacterController *characterController = new btKinematicCharacterController(mGhostObject,capsule,stepHeight);
	
	//characterController->setFallSpeed(50);
	//characterController->setGravity(40);
	//characterController->setJumpSpeed(20);
	//characterController->getGhostObject()->setFriction(50);

	//mCharacterController = characterController;

	//PhysicManager::Instance()->addCollisionObject(mGhostObject);
	//PhysicManager::Instance()->addCharacter(mCharacterController);

	btTransform trans;
	trans.setIdentity();      
	trans.setOrigin(btVector3(0,0,0));

	btDefaultMotionState* motionState =  new btDefaultMotionState(btVector3(00.0, 1.0, 00.0));

	mPhysicShape->setMargin(0.04);

	btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(1,motionState,mPhysicShape,btVector3(0,0,0));
	mRigidBody = new btRigidBody(rigidBodyCI);
	mRigidBody->setFriction(0.5f);
	//mRigidBody->setUserPointer(entityPointer);
	PhysicManager::Instance()->addRigidBody(mRigidBody);

	return true;
}

void Character::InitializeByXml(TiXmlElement* entityElement)
{
	mGame = Game::Instance();
	Initialize(NULL);//mGame->GetModel(0));
}

void Character::Uninit()
{

}

void Character::Update(int pElapsedTime)
{
	DIMOUSESTATE2 mouseState = Mouse::Instance()->getMouseState();
	mBodyOrientation += ((float)mouseState.lX) / 500;
	mHeadOrientation += ((float)mouseState.lY) / 500;

	XMStoreFloat4x4(&mHeadTransformation, XMMatrixRotationRollPitchYaw(mHeadOrientation,mBodyOrientation,0));

	btVector3 walkDirection = btVector3(0,0,0);
	if (Keyboard::Instance()->IsKeyDown(90))
	{
		walkDirection += btTransform(btQuaternion(mBodyOrientation,mHeadOrientation,0)) * btVector3(0.0, 0.0, 1.0);
	}
	else if (Keyboard::Instance()->IsKeyDown(83))
	{
		walkDirection -= btTransform(btQuaternion(mBodyOrientation,mHeadOrientation,0)) * btVector3(0.0, 0.0, 1.0);
	}
	if (Keyboard::Instance()->IsKeyDown(68))
	{
		walkDirection += btTransform(btQuaternion(mBodyOrientation + 1.57079,0,0)) * btVector3(0.0, 0.0, 1.0);
	}
	else if (Keyboard::Instance()->IsKeyDown(81))
	{
		walkDirection -= btTransform(btQuaternion(mBodyOrientation + 1.57079,0,0)) * btVector3(0.0, 0.0, 1.0);
	}

	btScalar walkVelocity = btScalar(0.1) * 4.0; // 4 km/h -> 1.1 m/s
	
	//if(mCharacterController->onGround())
	//{
		walkVelocity /= 2;
	//}
	//else
	//{
	//	walkVelocity /= 5;
	//}

	mWalkSpeed = (walkDirection * walkVelocity).length();

	if(abs(mWalkSpeed) < walkVelocity / 20)
	{
		mWalkSpeed = 0;
	}
	if(mWalkSpeed < 0)
	{
		mWalkSpeed += walkVelocity / 20; 
	}
	if(mWalkSpeed > 0)
	{
		mWalkSpeed -= walkVelocity / 20; 
	}
	if(mWalkSpeed > walkVelocity)
	{
		mWalkSpeed = walkVelocity;
	}
	if(mWalkSpeed < walkVelocity * -1)
	{
		mWalkSpeed = walkVelocity * -1;
	}

	mRigidBody->applyCentralForce(walkDirection * walkVelocity);
	mRigidBody->applyTorque(btVector3(0,0,0));
	//mCharacterController->setWalkDirection(walkDirection * walkVelocity);
	//mGhostObject->

	if (Keyboard::Instance()->IsKeyDown(32))
	{
		//mCharacterController->jump();
	}

}

void Character::PreRender()
{

}

void Character::Render()
{

}

XMFLOAT3 Character::getPosition()
{
	btTransform tr = mRigidBody->getWorldTransform(); 
	btVector3 pos = tr.getOrigin();
	return XMFLOAT3( pos.x(), pos.y(), pos.z() );
}

XMFLOAT3 Character::getHeadPosition()
{
	btTransform tr = mRigidBody->getWorldTransform(); 
	btVector3 pos = tr.getOrigin();
	return XMFLOAT3( (float)pos.x(), (float)(pos.y() + 0.6), (float)pos.z() );
}


float Character::getBodyOrienation()
{
	return mBodyOrientation;
}

float Character::getHeadOrienation()
{
	return mHeadOrientation;
}

XMFLOAT4X4* Character::GetHeadTransformation()
{
	return &mHeadTransformation;
}